import os
import argparse
import subprocess
from common import set_env


def arg_parse():
    parser = argparse.ArgumentParser(
        description='generate aicore operator model')

    parser.add_argument("-n",
                        dest='dev_num',
                        default=4,
                        type=int,
                        help="Number of miniD device")

    return parser.parse_args()


def set_ascend_env():
    """
    Set environment variable
    """
    set_env()

    os.environ['LD_LIBRARY_PATH'] = \
        os.environ['ASCEND_HOME'] + '/toolkit/lib64:' + \
        os.environ['LD_LIBRARY_PATH']
    os.environ['PATH'] = \
        os.environ['ASCEND_HOME'] + '/toolkit/tools/ide_daemon/bin:' + \
        os.environ['PATH']


if __name__ == "__main__":
    set_ascend_env()
    args = arg_parse()

    device_num = args.dev_num

    for i in range(device_num):
        print("device %d set log..." % i)
        list = [
            'adc', '--host', '127.0.0.1:22118', '--device', '%d' % (i),
            '--log', 'SetLogLevel(0)[error]'
        ]
        returnCode = subprocess.call(list, shell=False)
        if returnCode != 0:
            print("ERROR: cmd %s failed!" % list)
            break

        list = [
            'adc', '--host', '127.0.0.1:22118', '--device', '%d' % (i),
            '--log', 'SetLogLevel(2)[disable]'
        ]
        returnCode = subprocess.call(list, shell=False)
        if returnCode != 0:
            print("ERROR: cmd %s failed!" % list)
            break
